import axios from 'axios'
import config from 'config'
import httpFetch from 'share/httpFetch'
import errorMessage from "share/errorMessage";

export default {
  /**
   * appCode是否是有效的
   * @param appCode  子应用编码
   * @param id 子应用id
   * @return {*|AxiosPromise}
   */
  isValidAppCode(appCode, id){
    let params = {appCode: appCode, id: id};
    return httpFetch.post(`${config.baseUrl}/mobile/api/work/widget/verification`, params);
  },
  //注册子应用
  createSubApplication(data){
    return httpFetch.post(`${config.baseUrl}/mobile/api/work/widget`, data);
  },
  //编辑子应用
  updateSubApplication(data){
    return httpFetch.put(`${config.baseUrl}/mobile/api/work/widget`, data);
  },
  //查看子应用列表
  getSubApplicationList(params){
    return httpFetch.get(`${config.baseUrl}/mobile/api/work/widget`, params);
  },
  //查看子应用详情
  getSubApplication(id, manager){
    return httpFetch.get(`${config.baseUrl}/mobile/api/work/widget/${id}`, {manager: manager});
  },

  //管理端 获取当前租户下对应section可用列表
  getSectionList (sectionCode) {
    return  httpFetch.get(`${config.baseUrl}/mobile/api/management/widget/${sectionCode}`);
  },

  //新建/编辑租户子应用
  createNewSubApplication (params) {
    return httpFetch.post(`${config.baseUrl}/mobile/api/management/widget/tenant/draft`, params)
  },

  //获取租户子应用
  getTenantSubApplicationList (appCode) {
    return httpFetch.get(`${config.baseUrl}/mobile/api/management/widget/tenant/${appCode}`)
  },

  //获取租户全部子应用（分区域）
  getAllTenantSubApplicationList () {
    return httpFetch.get(`${config.baseUrl}/mobile/api/management/widget/tenant/all`)
  },

  //获取首页卡片应用详情
  getHomePageCardDetail () {
    return httpFetch.get(`${config.baseUrl}/mobile/api/management/config/tenant`)
  },

  //编辑首页卡片应用详情
  editHomePageCardDetail (item) {
    return httpFetch.post(`${config.baseUrl}/mobile/api/management/config/tenant`, item)
  },

  //默认子应用设置查询
  getDefaultApplication () {
    return httpFetch.get(`${config.baseUrl}/mobile/api/app/management/default/personalize/widget`)
  },

  //默认子应用设置更新
  updateDefaultApplication (arr) {
    return httpFetch.post(`${config.baseUrl}/mobile/api/app/management/default/personalize/widget`, arr)
  },

  //获取租户全部子应用（分区域）
  getEnabledSubApplicationList () {
    return httpFetch.get(`${config.baseUrl}/mobile/api/management/widget/tenant/all?isEnabled=true`)
  },
//新增子应用版本
  addVersionRecord () {
    return httpFetch.post(`${config.baseUrl}/mobile/api/widgetVersion/management/insert`)
  },

  //编辑子应用版本
  editVersionRecord (params) {
    return httpFetch.post(`${config.baseUrl}/mobile/api/widgetVersion/management/draft`, params)
  },

  //删除子应用版本
  deleteVersionRecord (id) {
    return httpFetch.get(`${config.baseUrl}/mobile/api/widgetVersion/management/delete?id=${id}`)
  },
  //删除子应用权限
  deleteSubApplicationLimit(sourceId, arr) {
    return new Promise(((resolve, reject) => {
      axios({
        method: 'delete',
        url: `${config.baseUrl}/mobile/api/widgetVersion/management/relation/delete/${sourceId}`,
        data: arr,
      }).then(res => {
        resolve(res);
      }).catch(err => {
        errorMessage(err.response);
        reject(err);
      });
    }))
  },

  //获取子应用权限
  getSubVersionUserLimit(sourceId, page) {
    return httpFetch.get(`${config.baseUrl}/mobile/api/widgetVersion/management/relation/list/${sourceId}?page=${page}&size=10`)
  },

  //获取子应用权限
  getVersionRecordList(appCode, page) {
    return httpFetch.get(`${config.baseUrl}/mobile/api/widgetVersion/management/list?appCode=${appCode}&page=${page}&size=10`)
  },

  //获取子应用权限
  getVersionRecord(sourceId) {
    return httpFetch.get(`${config.baseUrl}/mobile/api/widgetVersion/management/one?sourceId=${sourceId}`)
  },


  //获取子应用权限
  addSubApplicationLimit(params) {
    return httpFetch.post(`${config.baseUrl}/mobile/api/widgetVersion/management/relation`, params)
  },
}
